var table;
var layer;
var upload;
var form;
layui.use(['layer','table','upload','form'],function(){
    table=layui.table;
    layer=layui.layer;
    upload = layui.upload;
    form=layui.form;

    //上传图片
    upload.render({
        elem : '#uploadImgBtn',
        url : '/SyOperationController/uoloadCourse',//上传接口
        done : function(res) {
            if(res.code==0){

                //获取预览图个数
                var uploadImgNum=document.getElementsByClassName("uploadImg").length;

                //判断预览图是否大于8张，大于8张隐藏
                if(uploadImgNum>7){

                    //预览图计数
                    var num=$("#num").val();
                    if(num==0){
                        //隐藏之后，判断预览图是否大于8张，大于8张添加......
                        var num=document.getElementById('h1_id');
                        if(num!=null){
                            $("#h1_id").replaceWith("<h1 id='h1_id' onclick='uploadImg()' class='h'>......</h1>");
                            $("#img_div4").append("<img src='http://127.0.0.1:8089/course"+res.obj+"' class='uploadImg uploadImg2' style='display: none'/>");
                        }else{
                            $("#img_div4").append("<h1 id='h1_id' onclick='uploadImg()' class='h'>......</h1>");
                            $("#img_div4").append("<img src='http://127.0.0.1:8089/course"+res.obj+"' class='uploadImg uploadImg2' style='display: none'/>");
                        }
                    }else{
                        //判断......是否有，在进行块标签或隐藏
                        var num=document.getElementById('h1_id');
                        if(num!=null){
                            $("#img_div4").append("<img src='http://127.0.0.1:8089/course"+res.obj+"' class='uploadImg uploadImg2' style='display: none'/>");
                        }else{
                            $("#img_div4").append("<img src='http://127.0.0.1:8089/course"+res.obj+"' class='uploadImg uploadImg2' style='display: block'/>");
                        }
                    }
                }else{
                    //添加预览图，加父边框，变成行内块标签
                    $("#img_div2").attr("style","display:block");
                    $("#img_div4").append("<img src='http://127.0.0.1:8089/course"+res.obj+"' class='uploadImg'/>");
                    $("#img_div4").css({"border":"1px #D7D7D7 solid"});
                }
            }

            //获取主图
            var image=document.getElementsByClassName("uploadImg")[0].src;
            var url=image.substr(image.lastIndexOf('/'));
            $("#url").val(url);

        },
        drag : true,
        accept : 'file',
        multiple : true,
        data : {}
    });



    //上传视频
    upload.render({
        elem : '#uploadBtn2',
        url : '/SyOperationController/uoloadCourse',//上传接口
        done : function(res) {
            $("#div_video").append(" " +
                "<video id='my-video' class='video-js' controls preload='auto' data-setup='{}'> " +
                "<source src='http://127.0.0.1:8089/course/"+res.obj+"' class='uploadVideo' type='video/mp4'/> " +
                "</video>"
            );
            $("#courseUrl").val(res.obj);
        },
        drag : true,
        accept : 'video',
        multiple : true,
        data : { }
    });

});

//返回
function go() {
    history.go(-1)
}

//选择分类
function addProductCategory() {
    layer.open({
        type:2,
        title:'选择分类',
        area:['700px', '600px'],
        offset:'auto',
        shade:0.5,
        maxmin:true,
        content:"CourseCategory.html",
        success:function(layero,index){
            $("#p_id").attr("style","display:block");
        }
    });
    window.layer_1=layer;
}



//添加课程
layui.use('form',function () {
    var form=layui.form;
    form.on('submit(formDemo)', function(data){
        $.ajax({
            url:"/SyOperationController/addCourse",
            type:"post",
            dataType:"json",
            traditional: true,//ajax传递数组必须添加属性
            data:formData,
            success:function(data){
                if(data.code==0){
                    layer.msg('添加成功');
                }else{
                    layer.msg('添加失败');
                }
            }
        });
        return false;
    });

});

//上传课程视频，进行判断
function uploadBtn(){
    //判断是否已有视频，在进行上传
    var num=$("#my-video").length;
    if(num>=1){
        layer.msg("您已添加过课程", {icon: 6});
    }else{
        $('#uploadBtn2').click();
    }
}

//预览图点击展开
function uploadImg() {
    $("#up").attr("style","display:block");
    //预览图点击计数
    var num=0;
    num++;
    $("#num").val(num);
    $(".uploadImg2").attr("style","display:block");
    $("h1").remove(".h");
}

//收起预览图
function upUploadImg() {
    $(".uploadImg2").attr("style","display:none");
    $("#up").attr("style","display:none");
    $("#img_div4").append("<h1 id='h1_id' onclick='uploadImg()' class='h'>......</h1>");
}



//添加课程
layui.use('form',function () {
    var form=layui.form;
    form.on('submit(formDemo)', function(data){

        //处理预选图
        var str=[];
        $("#img_div4 img").each(function (a) {

            //获取图片后面路径 截取拼接
            var arrayImgs= ($(this).attr("src"));
            var img=arrayImgs.substr(arrayImgs.lastIndexOf('/'));

            var courseName=$("#courseName").val();
            json={"pictureType":2,"url":img,"pictureName":courseName};

            str.push(json);
        });

        //获取预览图
        var formData = data.field;
        formData.str=JSON.stringify(str);

        $.ajax({
            url:"/SyOperationController/addCourse",
            type:"post",
            dataType:"json",
            traditional: true,//ajax传递数组必须添加属性
            data:formData,
            success:function(data){
                if(data.code==0){
                    layer.msg('添加成功');
                }else{
                    layer.msg('添加失败');
                }
            }
        });
        return false;
    });

});